package com.triones.ljr.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtil;
import com.triones.ljr.entity.dto.AdductDO;
import com.triones.ljr.entity.vo.AdductVO;
import com.triones.ljr.mapper.AdductMapper;
import com.triones.ljr.service.AdductSerivce;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 政策汇集
 */
@Service
@RequiredArgsConstructor
public class AdductSerivceImpl implements AdductSerivce {

    private final AdductMapper adductMapper;

    @Override
    public TableDataInfo<AdductVO> pageList(AdductDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<AdductDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(AdductDO::getIsDel,"0")
            .eq(ObjectUtil.isNotEmpty(dto.getType()),AdductDO::getType,dto.getType())
            .like(ObjectUtil.isNotEmpty(dto.getName()),AdductDO::getName,dto.getName())
            .orderByAsc(AdductDO::getSort);
        Page<AdductVO> result = adductMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public boolean add(AdductDO dto) {
        dto.setCreateTime(DateUtil.getDateTimeString());
        return adductMapper.insert(dto) > 0;
    }

    @Override
    public boolean edit(AdductDO dto) {
        return adductMapper.updateById(dto) > 0;
    }

    @Override
    public AdductVO getInfo(Long id) {
        return adductMapper.selectVoById(id);
    }

    @Override
    public boolean remove(Long id) {
        AdductDO adductDO = adductMapper.selectById(id);
        if (adductDO == null) {
            throw new SecurityException("没有查询到信息");
        }
        adductDO.setIsDel("1");
        return adductMapper.updateById(adductDO) > 0;
    }

    @Override
    public boolean checkNameUnique(AdductDO dto) {
        boolean exist = adductMapper.exists(new LambdaQueryWrapper<AdductDO>()
            .eq(AdductDO::getName, dto.getName())
            .eq(AdductDO::getIsDel, "0")
            .ne(ObjectUtil.isNotNull(dto.getId()), AdductDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public List<AdductVO> list(Integer type) {
        return adductMapper.selectVoList(new LambdaQueryWrapper<AdductDO>()
            .eq(AdductDO::getIsDel,"0").eq(AdductDO::getType, Convert.toStr(type)));
    }
}
